<!-- 新增-门店-->
<template>
  <div class="addStore">
    <CommonBtn label-name="门店信息新增列表" v-if="!button || (button.event!='add()' && button.event!='edit()') " :buttons="buttons" @onButtonClick="onButtonClick">
      <span slot="conditions" v-for="(item,index) of conditions" :key="index" v-if="item.checked && item.label && item.valueName != undefined">{{item.label+":"+item.valueName}}</span>
    </CommonBtn>
    <CommonList ref="commonList" v-if="!showEdit" :buttons="buttons"
                :columns="columns" :listUrl="listUrl" :conditions="conditions" @tableSelect="tableSelect" @dbClick="openItem"></CommonList>
    <StoreEdit :pageType="pageType"
                  ref="storeEdit"
                  :disabled="!enableEdit"
                  :label="editTitle"
                  v-if="showEdit"
                  :items="items"
                  :originalData="row"
                  @editBack="editBack"
                  @search="search"
                  @submit="submit" />
    <CommonSearch :label="item.label"
                  :conditions="item.conditions"
                  v-for="(item, index) of searchs"
                  :key="index"
                  :listUrl="item.listUrl"
                  :columns="item.columns"
                  :items="item.items"
                  :page="item.page"
                  :originalData="item.searchData"
                  @search="search"
                  @submit="item.selectItem"
                  @searchBack="searchBack"/>
    <CommonInport :out-btn="button" @close="close" :inportUrl="inportUrl" :template-url="templateUrl" importMsg="insert"></CommonInport>
    <CommonExport :out-btn="button" :props="columns" :export_type="export_type"
                  @close="close" :export-url="$api.API_RETAILER_EXPORT" :conditions="conditions"></CommonExport>
  </div>
</template>

<script>
    import http from '@/api/http'
    import {Message } from 'element-ui'
    import CommonList from "../../common/CommonList"
    import CommonBtn from "../../common/CommonBtn";
    import CommonSearch from '../../common/CommonSearch'
    import StoreEdit from "./StoreEdit";
    import CommonInport from "../../common/CommonInport";
    import CommonExport from "../../common/CommonExport";
    export default {
        name:'addStore',
        data(){
            return{
              searchData: {},
              searchItems: [],
                inportUrl: "",
                listUrl: this.$api.API_SELECT_RETAILER_LIST,
                button: null,
                conditions: [{
                    checked: true,
                    prop: 'pageType',
                    value: '0'
                }],
                export_type: [{
                    checked: true,
                    prop: 'pageType',
                    value: '0'
                }],
                row: null,
                searchs: [],
                pageType: 0,
                showEdit: false,
                enableEdit: false,
                editTitle: '',
                buttons:[
                    //门店批量导入
                    this.$config.BTNS.STOREUPDATE,
                    //POS门店导入
                    this.$config.BTNS.POSINPORT,
                    this.$config.BTNS.ADD,
                    this.$config.BTNS.EDIT,
                    this.$config.BTNS.DELETE,
                    this.$config.BTNS.EXPORT,
                    this.$config.BTNS.SEARCH
                ],
                columns:[
                    {
                        prop:'code',
                        label:'外部系统编码'
                    },
                    {
                        prop:'retailName',
                        label:'门店名称'
                    },
                    {
                        prop:'wfStatus',
                        label:'节点状态'
                    },
                    {
                        prop:'dataSource',
                        label:'数据来源'
                    },
                    {
                        prop:'retailGroup',
                        label:'所属集团客户'
                    },
                    {
                        prop:'isLKAvalue',
                        label:'是否LKA'
                    },
                    {
                        prop:'retailType',
                        label:'门店类型'
                    },
                    {
                        prop:'locationName',
                        label:'Location'
                    },
                    {
                        prop:'province',
                        label:'省'
                    },
                    {
                        prop:'cityName',
                        label:'市'
                    },
                    {
                        prop:'adminCity',
                        label:'区县'
                    },
                    {
                        prop:'deAddress',
                        label:'详细地址'
                    },
                    {
                        prop:'repName',
                        label:'负责人'
                    },
                    {
                        prop:'remark',
                        label:'Remark'
                    },
                    {
                        prop:'createTime',
                        label:'创建时间'
                    },
                    {
                        prop:'modifyTime',
                        label:'最后修改时间'
                    },
                ],
                items: [
                {
                    name: '统一编码',
                    key: 'mdmCode',
                    type: 'text',
                    disable: true,
                    required: false,
                    placeholder: '待生成'
                },
                {
                  name: '',
                  key: '',
                  type: 'empty',
                  disable: true,
                  required: false
                },
                {
                  name: '外部系统编码',
                  key: 'code',
                  type: 'checktext',
                  disable: false,
                  required: true,
                  placeholder: '',
                  checkname: '是否自动生成'
                },
                {
                  name: '门店名称',
                  key: 'retailName',
                  type: 'text',
                  disable: false,
                  required: true,
                  placeholder: ''
                },
                {
                  name: '地理城市',
                  key: 'city',
                  type: 'multi-select',
                  required: false,
                  optionss: [{
                    options: [],
                    key: 'province'
                  }, {
                    options: [],
                    key: 'physicalCity'
                  }, {
                    options: [],
                    key: 'adminCity'
                  }]
                },
                {
                  name: '城市类别',
                  key: 'cityTypeName',
                  vkey: 'fkCityType',
                  type: 'single-select',
                  required: false,
                  disable: true,
                  options: []
                },
                {
                  name: '详细地址',
                  key: 'deAddress',
                  type: 'multi-text',
                  required: false,
                  texts: [{
                    key: 'address',
                    name: '街道',
                    span: 12
                  }, {
                    key: 'address1',
                    name: '路',
                    span: 12
                  }, {
                    key: 'address2',
                    name: null,
                    span: 24
                  }]
                },
                {
                  name: '经纬度',
                  type: 'location',
                  key1: 'longitude',
                  key2: 'latitude',
                  disable: true,
                },
                {
                  name: '门店类型',
                  key: 'retailType',
                  vkey: 'fkRetailType',
                  type: 'single-select',
                  required: false,
                  disable: false,
                  options: []
                },
                {
                  name: 'Location',
                  key: 'locationName',
                  vkey: 'fkLocation',
                  type: 'single-select',
                  required: false,
                  disable: false,
                  options: []
                },
                {
                  name: '门店投资属性',
                  key: 'levelName',
                  vkey: 'fkLevel',
                  type: 'single-select',
                  required: false,
                  disable: true,
                  options: []
                },
                {
                  name: '所属客户集团',
                  key: 'retailGroup',
                  type: 'search',
                  required: false,
                  disable: false
                },
                {
                  name: '是否LKA',
                  key: 'isLKAvalue',
                  vkey: 'isLKA',
                  type: 'single-select',
                  required: false,
                  disable: false,
                  placeholder: '请选择',
                  options: []
                },
                {
                  name: '强生负责人',
                  key: 'repName',
                  vkey: 'fkRep',
                  type: 'search',
                  required: true,
                  disable: false
                },
                {
                  name: '门店联系人姓名',
                  key: 'linkMan',
                  type: 'text',
                  required: false,
                  disable: false
                },
                {
                  name: '门店联系人电话',
                  key: 'tel',
                  type: 'text',
                  required: false,
                  disable: false
                },
                {
                  name: '门店联系人邮箱',
                  key: 'retailerMail',
                  type: 'text',
                  required: false,
                  disable: false,
                  placeholder: '多个邮箱请用英文,(逗号分割)'
                },
                {
                  name: '门店营业年月',
                  key: 'businessTime',
                  type: 'date',
                  year: 'businessYear',
                  month: 'businessMonth',
                  options: [],
                  required: true,
                  disable: false,
                  placeholder: '请选择'
                },
                {
                  name: '营业状态',
                  key: 'businessStatus',
                  vkey: 'businessStatus',
                  type: 'single-select',
                  required: false,
                  disable: false,
                  placeholder: '请选择'
                },
                {
                  name: '合作状态',
                  key: 'cooperationStatus',
                  vkey: 'cooperationStatus',
                  type: 'single-select',
                  required: false,
                  disable: true,
                  placeholder: '请选择'
                },
                {
                  name: 'Destination',
                  key: 'destinationName',
                  vkey: 'fkDestination',
                  type: 'single-select',
                  required: false,
                  disable: true,
                  placeholder: '请选择'
                },
                {
                  name: 'Remark',
                  key: 'remark',
                  type: 'text',
                  required: false,
                  disable: false
                },
                {
                  name: '是否Promoter覆盖',
                  key: 'isPromoter',
                  type: 'checkbox',
                },
                {
                  name: '是否B2B生意覆盖',
                  key: 'isB2b',
                  type: 'checkbox',
                },
                {
                  name: '是否销售强生商品',
                  key: 'isSalesjnj',
                  type: 'checkbox',
                }],
                templateUrl : "/static/template/门店导入模板.xls"
            }
        },
        methods:{
            onButtonClick(item) {
                if (item.event == 'Delete()') {
                  if (!this.row || JSON.stringify(this.row) == '{}') {
                    this.$message.error("请选择一行数据")
                    return
                  }
                  this.$confirm('您确认要删除门店 “' + this.row.retailName + '” 吗？', '提示', {
                    confirmButtonText: '确认删除',
                    cancelButtonText: '取消',
                    type: 'warning'
                  }).then(() => {
                    http.post(this.$api.API_STORE_DELETE, {pkid: this.row.pkid})
                        .then(resp=>{
                          Message({type: 'success', message: '门店已删除！'})
                          this.$refs.commonList.getList()
                        })
                  }).catch(() => {});
                  return;
                }
                this.enableEdit = true
                if (item.event == 'edit()') {
                  if (!this.row || JSON.stringify(this.row) == '{}') {
                    this.$message.error("请选择一行数据")
                    return
                  }
                  this.editTitle = '编辑门店信息'
                  this.showEdit = true
                  this.items[2].disable = true
                }

                if (item.event == 'add()') {
                    this.showEdit = true
                    this.editTitle = '新增门店信息'
                    this.row = null
                    this.items[2].disable = false
                }
                if(item == this.$config.BTNS.STOREUPDATE){
                    this.inportUrl=this.$api.API_RETAILER_BATCHIMPORT
                    this.templateUrl = "/static/template/门店导入模板.xls"
                }
                if(item == this.$config.BTNS.POSINPORT){
                    this.inportUrl=this.$api.API_POSRETAILER_IMPORT;
                    this.templateUrl = "/static/template/POS门店导入模板.xls"
                }
                let self = this
                if (item.event == 'search()') {
                  this.searchs.push({
                    label: '查询门店信息',
                    page: 'store',
                    searchData: this.searchData,
                    items: this.searchItems,
                    selectItem: function (formData, items) {
                      self.searchs.pop()
                      self.searchItems = items;
                      self.searchData = formData;
                      let conditions = self.conditions.filter(item=>{
                        return !item.label
                      })
                      // 回调
                      for (let idx in items) {
                        let item = items[idx]
                        if (item.checked == true) {
                          conditions.push({
                            checked: true,
                            label: item.name,
                            prop: item.vkey ? item.vkey : item.key,
                            valueName: formData[item.key],
                            value: item.vkey ? formData[item.vkey] : formData[item.key]
                          })
                        }
                      }
                      self.conditions = conditions
                    },
                  })
                }
                this.button = item
            },
            search(item, callback) {
              let self = this
              if (item.key == 'soldToCode') {
                this.searchs.push({
                  label: '客户信息',
                  conditions: [{checked: true, prop: 'pageType', value: '9'},{checked: true, prop: 'fkDealerLevel', value: this.$refs.storeEdit.formData.fkDealerLevel}],
                  listUrl: this.$api.API_SMMS_DEALER_LIST,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof callback == 'function') {
                      callback(item)
                    }
                  },
                  items: [{
                    name: '客户名称',
                    key: 'smmsName',
                    type: 'text',
                  },{
                    name: 'SoldToCode',
                    key: 'soldToCode',
                    type: 'text',
                  },{
                    name: '统一编码',
                    key: 'smmsCode',
                    type: 'text',
                  },{
                    name: '客户属性',
                    key: 'levelName',
                    type: 'search',
                  },{
                    name: '客户集团',
                    key: 'customNameCn',
                    type: 'search',
                  },{
                    name: '客户负责人',
                    key: 'repName',
                    type: 'search',
                  }],
                  columns: [{
                    prop: 'smmsName',
                    label: '客户名称'
                  },{
                    prop: 'soldToCode',
                    label: 'SoldToCode'
                  },{
                    prop: 'smmsCode',
                    label: '统一编码'
                  },{
                    prop: 'levelName',
                    label: '客户属性'
                  },{
                    prop: 'customNameCn',
                    label: '客户集团'
                  },{
                    prop: 'repName',
                    label: '客户负责人'
                  }]
                })
              } else if (item.key == 'levelName') {
                // 客户属性
                this.searchs.push({
                  label: '客户属性管理',
                  conditions: [],
                  listUrl: this.$api.API_CUSTOMERTYPE,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      callback({levelName: item.levelName, fkDealerLevel: item.pkid})
                    }
                  },
                  items: [{
                    name: '客户属性名称',
                    key: 'levelName',
                    type: 'text',
                  },{
                    name: '客户属性代码',
                    key: 'levelCode',
                    type: 'text',
                  },{
                    name: '是否有效',
                    key: 'isValid',
                    type: 'checkbox',
                  }],
                  columns: [{
                    prop: 'levelName',
                    label: '客户属性名称'
                  },{
                    prop: 'levelCode',
                    label: '客户属性代码'
                  },{
                    prop: 'valid',
                    label: '是否有效'
                  }]
                })
              } else if (item.key == 'repName' || item.key == 'sfaRepName') {
                // 客户属性
                this.searchs.push({
                  label: '人员信息',
                  conditions: [],
                  listUrl: this.$api.API_GETTPINFO_LIST,
                  selectItem: function (itemData) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      let data= {}
                      data[item.key] = itemData.repName
                      if (item.vkey) {
                        data[item.vkey] = itemData.pkid
                      } else {
                        data.fkRep = itemData.pkid
                      }
                      callback(data)
                    }
                  },
                  items: [{
                    name: '员工姓名',
                    key: 'repName',
                    type: 'text',
                  },{
                    name: '员工账号',
                    key: 'repCode',
                    type: 'text',
                  },{
                    name: '所属管理层级',
                    key: 'positionName',
                    type: 'search',
                  },{
                    name: 'WWID',
                    key: 'wWID',
                    type: 'text',
                  },{
                    name: 'SAP代码',
                    key: 'sapCode',
                    type: 'text',
                  },{
                    name: '总部/大区',
                    key: 'hQOrRegion',
                    type: 'text',
                  }],
                  columns: [{
                    prop: 'repName',
                    label: '员工姓名'
                  },{
                    prop: 'repCode',
                    label: '员工账号'
                  },{
                    prop: 'positionName',
                    label: '所属管理层级'
                  },{
                    prop: 'WWID',
                    label: 'wWID'
                  },{
                    prop: 'sapCode',
                    label: 'SAP代码'
                  },{
                    prop: 'hQOrRegion',
                    label: '总部/大区'
                  }]
                })
              } else if (item.key == 'retailGroup' || item.key == 'customNameCn') {
                // 客户集团
                this.searchs.push({
                  label: '客户集团',
                  conditions: [],
                  listUrl: this.$api.API_SELECT_CUSTOMER_GROUP_INFO,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      callback({retailGroup: item.customNameCN, fkRetailGroup: item.pkid, customNameCn: item.customNameCN, fkCustom: item.pkid})
                    }
                  },
                  items: [{
                    name: '集团中文名称',
                    key: 'customNameCN',
                    type: 'text',
                  },{
                    name: '客户集团编码',
                    key: 'customCode',
                    type: 'text',
                  },{
                    name: '集团英文名称',
                    key: 'customNameEN',
                    type: 'text',
                  },{
                    name: '预算Account',
                    key: 'accountCode',
                    type: 'text',
                  },{
                    name: '客户集团分类',
                    key: 'customTypeName',
                    type: 'search',
                  },{
                    name: '是否有效',
                    key: 'isValid',
                    type: 'checkbox',
                  }],
                  columns: [{
                    prop: 'customNameCN',
                    label: '集团中文名称'
                  },{
                    prop: 'destinationName',
                    label: 'Destination'
                  },{
                    prop: 'customNameEN',
                    label: '集团英文名称'
                  },{
                    prop: 'accountCode',
                    label: '预算Account'
                  },{
                    prop: 'customTypeName',
                    label: '客户集团分类'
                  },{
                    prop: 'valid',
                    label: '是否有效'
                  }]
                })
              } else if (item.key == 'positionName') {
                // 职位信息
                this.searchs.push({
                  label: '职位信息',
                  conditions: [],
                  listUrl: this.$api.API_MANAGEMENTHIERARCHY_LIST,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      callback({positionName: item.positionName, fkPosition: item.pkid})
                    }
                  },
                  items: [{
                    name: '职位名称',
                    key: 'positionName',
                    type: 'text',
                  },{
                    name: '职位代码',
                    key: 'positionCode',
                    type: 'text',
                  }],
                  columns: [{
                    prop: 'positionName',
                    label: '职位名称'
                  },{
                    prop: 'createTime',
                    label: '创建时间'
                  },{
                    prop: 'modifyTime',
                    label: '最后修改时间'
                  },{
                    prop: 'isValid',
                    label: '是否有效'
                  },{
                    prop: 'positionCode',
                    label: '职位代码'
                  }]
                })
              } else if (item.key == 'customTypeName') {
                // 客户集团分类
                this.searchs.push({
                  label: '客户集团分类',
                  conditions: [],
                  listUrl: this.$api.API_CUSTOMERCLASSIFY_LIST,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      callback({customTypeName: item.customTypeName, fkCustomType: item.pkid})
                    }
                  },
                  items: [{
                    name: '集团类型编码',
                    key: 'customTypeCode',
                    type: 'text',
                  },{
                    name: '集团类型名称',
                    key: 'customTypeName',
                    type: 'text',
                  },{
                    name: '是否有效',
                    key: 'isValid',
                    type: 'checkbox',
                  }],
                  columns: [{
                    prop: 'customTypeCode',
                    label: '集团类型编码'
                  },{
                    prop: 'customTypeName',
                    label: '集团类型名称'
                  },{
                    prop: 'valid',
                    label: '是否有效'
                  }]
                })
              } else if (item.key == 'payLinkCode') {
                // PayLink编码
                this.searchs.push({
                  label: '',
                  conditions: [],
                  listUrl: this.$api.API_STORE_PAYLINK_LIST,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      callback({payLinkCode: item.payLinkCode, payLinkName: item.payLinkName, fkPayLink: item.pkid})
                    }
                  },
                  items: [{
                    name: 'PayLink编码',
                    key: 'payLinkCode',
                    type: 'text',
                  },{
                    name: 'PayLink名称',
                    key: 'payLinkName',
                    type: 'text',
                  }],
                  columns: [{
                    prop: 'payLinkCode',
                    label: 'PayLink编码'
                  },{
                    prop: 'payLinkName',
                    label: 'PayLink名称'
                  }]
                })
              }
            },
            editBack() {
                this.showEdit = false
                this.button = null
            },
            tableSelect(item) {
              this.row = item
              this.button = null
            },
            openItem(item) {
              // 打开详情
              this.showEdit = true
              this.enableEdit = false
              this.editTitle = '查看门店信息'
            },
            searchBack() {
              this.searchs.pop()
            },
            submit(item) {
              item.pageType = this.pageType
              if (this.row) {
                // 编辑
                http.post(this.$api.API_STORE_INFO_UPDATE, item)
                    .then(resp=>{
                  Message({type: 'success',message: '保存成功'});
                  this.row = null
                  this.editBack()
                })
              } else {
                // 新增
                http.post(this.$api.API_STORE_INFO_SAVE, item)
                .then(resp=>{
                  Message({type: 'success',message: '保存成功'});
                  this.row = null
                  this.editBack()
                })
              }
            },
            close(item) {
                this.button = item
            }
        },
        watch: {
          showEdit(newVal,oldVal) {
            if (!newVal && oldVal) {
              this.conditions = this.conditions.map(item=>{return item})
            }
          }
        },
        components:{CommonExport,CommonInport,CommonList,CommonBtn, StoreEdit, CommonSearch}
    }
</script>
